package modele;

import java.awt.Color;
import java.util.LinkedList;

public class Rectangle extends FormeGeometrique {
	
	private Point premierPoint = null;
	private Point deuxiemePoint = null;
	
	public Rectangle(){
		this.premierPoint = null;
		this.deuxiemePoint = null;
	}
	
	public Rectangle(Point arg0, Point arg1){
		this.premierPoint=arg0;	
		this.deuxiemePoint=arg1;
		}	
	public Rectangle(Point arg0, Point arg1, Color c){
		this.premierPoint=arg0;	
		this.deuxiemePoint=arg1;
		this.couleur = c;
		}	
	
	public Point getPremierPoint() {return premierPoint;}
	public Point getDeuxiemePoint() {return deuxiemePoint;}
	public void setPremierPoint(Point premierPoint) {this.premierPoint = premierPoint;}
	
	public void setDeuxiemePoint(Point deuxiemePoint) {
		this.deuxiemePoint = deuxiemePoint;
	}
	public int getWidth(){return (deuxiemePoint.getX()-premierPoint.getX());}
	public int getHeight(){return (deuxiemePoint.getY()-premierPoint.getY());}

	@Override
	public FormeGeometrique createClone() {
		return new Rectangle(this.premierPoint.createClone(), this.deuxiemePoint.createClone(),this.couleur);
	}

	@Override
	public FormeGeometrique createSymetrieO(Point origine) {
		return new Rectangle(this.deuxiemePoint.createSymetrieO(origine), this.premierPoint.createSymetrieO(origine), this.getCouleur());
	}

	@Override
	public FormeGeometrique createSymetrieX(Point origine) {
		int height = this.getHeight();
		Point premier = this.premierPoint.createSymetrieX(origine);
		Point deuxieme = this.deuxiemePoint.createSymetrieX(origine);
		premier.setY(premier.getY()-height);
		deuxieme.setY(deuxieme.getY()+height);
		return new Rectangle(premier, deuxieme, this.couleur);
	}

	@Override
	public FormeGeometrique createSymetrieY(Point origine) {
		int width = this.getWidth();
		Point premier = this.premierPoint.createSymetrieY(origine);
		Point deuxieme = this.deuxiemePoint.createSymetrieY(origine);
		premier.setX(premier.getX()-width);
		deuxieme.setX(deuxieme.getX()+width);
		return new Rectangle(premier,deuxieme, this.couleur );
	}

	@Override
	public boolean equals(FormeGeometrique o) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public double perimetre() {
		return (2*getHeight()+2*getWidth());
	}

	@Override
	public double surface() {
		return (getWidth()*getWidth());
	}

	@Override
	public void symetrieO(Point origine) {
		
		this.premierPoint.symetrieO(origine);
		this.deuxiemePoint.symetrieO(origine);
		//ensuite il faut switcher le premier et le deuxieme point
		Point temp = this.premierPoint;
		this.setPremierPoint(this.deuxiemePoint);
		this.setDeuxiemePoint(temp);
	}

	@Override
	public void symetrieX(Point origine) {
		int height = this.getHeight();
		this.premierPoint.symetrieX(origine);
		this.deuxiemePoint.symetrieX(origine);
		this.getPremierPoint().setY(this.getPremierPoint().getY()-height);
		this.getDeuxiemePoint().setY(this.getDeuxiemePoint().getY()+height);

	}

	@Override
	public void symetrieY(Point origine) {
		int width = this.getWidth();
		this.premierPoint.symetrieY(origine);
		this.deuxiemePoint.symetrieY(origine);
		//ensuite il faut décaler le point 2 et décaler le point 1
		this.getPremierPoint().setX(this.getPremierPoint().getX()-width);
		this.getDeuxiemePoint().setX(this.getDeuxiemePoint().getX()+width);
	}




	public void switchPoint(Point base) {
		Point clonepremz = base;
		Point clonedeuz = this.deuxiemePoint.createClone();
		int x1 = clonepremz.getX();
		int x2 = clonedeuz.getX();
		int y1 = clonepremz.getY();
		int y2 = clonedeuz.getY();
		
		if (x1<x2) {
			this.premierPoint.setX(x1);
			this.deuxiemePoint.setX(x2);
		}
		else {
			this.premierPoint.setX(x2);
			this.deuxiemePoint.setX(x1);
		}
		
		if (y1 < y2) {
			this.premierPoint.setY(y1);
			this.deuxiemePoint.setY(y2);
		}
		else{
			this.premierPoint.setY(y2);
			this.deuxiemePoint.setY(y1);
		}
		
		
	}
	
	
	public LinkedList<Segment> getSegments(){
		LinkedList<Segment> lesSegments = new LinkedList<Segment>();
				
		lesSegments.add(new Segment(this.premierPoint, new Point(premierPoint.getX(),deuxiemePoint.getY())));
		lesSegments.add(new Segment(this.premierPoint, new Point(deuxiemePoint.getX(),premierPoint.getY())));
		
		lesSegments.add(new Segment(this.deuxiemePoint, new Point(premierPoint.getX(),deuxiemePoint.getY())));
		lesSegments.add(new Segment(this.deuxiemePoint, new Point(deuxiemePoint.getX(),premierPoint.getY())));

		
		return lesSegments;
		
	}

	@Override
	public void deplacer(Point depart, Point destination) {
		this.premierPoint.deplacer(depart, destination);
		this.deuxiemePoint.deplacer(depart, destination);	
	}

	@Override
	public boolean isOnTheClick(Point clic) {
		if (clic.getX()>this.getPremierPoint().getX()
				&& clic.getX()<this.getDeuxiemePoint().getX()
				&& clic.getY()>this.getPremierPoint().getY()
				&& clic.getY()<this.getDeuxiemePoint().getY()
				) return true;
		
		return false;
		
//		for (Segment item : this.getSegments()) {
//			if (isClickOnSegment(item, clic)) {return true;}
//		}
//		
//		return false;
	}

}
